package cn.jinbyte.core.exception;

import cn.jinbyte.core.model.ApiCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

import static cn.jinbyte.core.model.ApiCode.BS_ERROR;


@EqualsAndHashCode(callSuper = true)
@Data
public class BussException extends RuntimeException implements SysError {
    private final ApiCode code;
    private final String message;

    public BussException(ApiCode code) {
        this.code = code;
        this.message = code.getDescription();
    }

    public BussException(String message) {
        this.code = BS_ERROR;
        this.message = message;
    }

    public BussException(ApiCode code, String message) {
        this.code = code;
        this.message = message;
    }

    public static void throwIf(boolean condition, String template, Object... args) {
        if (condition) {
            String message = java.lang.String.format(template, args);
            throw new BussException(message);
        }
    }
}
